const mongoose = require('mongoose');
const BaseModel = require("./base_model");
const Schema = mongoose.Schema;

const UserSchema = new Schema({
    name: { type: String }, //用户名
    loginName: { type: String }, //登录名
    pass: { type: String }, //密码
    email: { type: String }, //邮箱
    url: { type: String }, //个人网站地址
    avatar: { type: String }, //头像链接地址
    level: { type: String }, //等级
    active: { type: Boolean, default: false}, //账号是否激活

    profile_image_url: {type: String},
    location: { type: String }, //所在地点
    signature: { type: String }, //签名
    profile: { type: String },
    weibo: { type: String },
    githubId: { type: String},
    githubUsername: {type: String},
    githubAccessToken: {type: String},
    is_block: {type: Boolean, default: false},

    score: { type: Number, default: 0 }, //积分
    topic_count: { type: Number, default: 0 }, //发布的话题数
    reply_count: { type: Number, default: 0 }, //参数的评论数
    follower_count: { type: Number, default: 0 },
    following_count: { type: Number, default: 0 },
    collect_tag_count: { type: Number, default: 0 },
    collect_topic_count: { type: Number, default: 0 }, //收藏的主题数
    create_at: { type: Date, default: Date.now }, //注册时间
    update_at: { type: Date, default: Date.now }, //信息更新时间
    is_star: { type: Boolean },
    receive_reply_mail: {type: Boolean, default: false },
    receive_at_mail: { type: Boolean, default: false },
    from_wp: { type: Boolean },

    retrieve_time: {type: Number},
    retrieve_key: {type: String},

    accessToken: {type: String},

    sex: { type: String }, // 性别
    love_state: { type: String } //恋爱状态

});

UserSchema.plugin(BaseModel);

UserSchema.virtual('isAdvanced').get(function () {
    // 积分高于 700 则认为是高级用户
    return this.score > 700 || this.is_star;
});

UserSchema.index({loginName: 1}, {unique: true});
UserSchema.index({email: 1}, {unique: true});
UserSchema.index({score: -1});
UserSchema.index({githubId: 1});
UserSchema.index({accessToken: 1});

UserSchema.pre('save', function(next) {
    let now = new Date();
    this.update_at = now;
    next();
});
mongoose.model('User', UserSchema);